package com.burgeon.backend.Rest.User;
/**
 * 获取零售信息通过vip卡号
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.RetailRecord;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;
import java.util.*;

public class GetRetailByVip extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetRetailByVip(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList<RetailRecord> getData(String vip) throws Exception {
        String url = "http://"+ip+"/servlets/binserv/Rest";

        String str = RESULT(Data(vip),url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        String message = new JSONArray(str).getJSONObject(0).getString("message");
        if (!"0".equals(code)){
           throw new Exception(message);
        }
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<RetailRecord> data = new ArrayList();
        for (int i =0;i<jsonArray.length();i++){
            JSONArray array = jsonArray.getJSONArray(i);
            RetailRecord retailRecord = new RetailRecord();
            retailRecord.setId(array.getString(0));
            retailRecord.setDate(array.getString(1).substring(0,array.getString(1).length()-2));
            retailRecord.setCode(array.getString(2));
            retailRecord.setType(array.getString(3));
            retailRecord.setStore(array.getString(4));
            retailRecord.setPrice(array.getString(5));
            data.add(retailRecord);
        }

        return data;
    }

    private MultiValueMap Data(String vip) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("ID");
        columns.add("STATUSTIME");
        columns.add("DOCNO");
        columns.add("RETAILBILLTYPE");
        columns.add("'C_STORE_ID;NAME'");
        columns.add("TOT_AMT_ACC");


        params.put("columns", columns);
        params.put("table", "M_RETAIL");//表名

        params.put("count","true");

        HashMap orderby = new HashMap();

        orderby.put("column","OPENCARDDATE");
        orderby.put("asc","false");

        HashMap paramss = new HashMap();
//        String ac = "'"+ vip+"'";
//        paramss.put("column", "'C_VIP_ID;MOBIL'");
//        paramss.put("condition", ac);
//        params.put("params", paramss);
        //*************************************
        HashMap expr1= new HashMap();

        HashMap expr2= new HashMap();

        expr2.put("column", "'STATUS'");
        expr2.put("condition", 2);

        HashMap expr1v1= new HashMap();

        expr1v1.put("column", "'C_VIP_ID;MOBIL'");
        expr1v1.put("condition", vip);
        expr1.put("expr1",expr1v1);

        HashMap expr1v2= new HashMap();

        expr1v2.put("column", "'ISACTIVE'");
        expr1v2.put("condition", "Y");
        expr1.put("expr2",expr1v2);
        expr1.put("combine","and");

        paramss.put("combine", "and");//关系
        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);
        //*************************************

        params.put("params", paramss);
        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        GetRetailByVip getRetailByVip = new GetRetailByVip("39.99.239.133:55321", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(getRetailByVip.getData("17732141036"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
